package name.sakanacatcher.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.*;

@Entity
@Table(name = "user")
@JsonIgnoreProperties({ "handler", "hibernateLazyInitializer" })
public class User implements UserDetails {

    public User() {
        avatar = "http://localhost:8080/1.jpg";
    }

    public User(String username, String name, String gender, String password, String avatar, String status, String intro) {
        this.username = username;
        this.name = name;
        this.gender = gender;
        this.password = password;
        this.avatar = avatar;
        this.status = status;
        this.intro = intro;
    }

    public User(int id, String username, String name, String gender,  String password, String avatar, String status, String intro) {
        this.id = id;
        this.username = username;
        this.name = name;
        this.gender = gender;
        this.password = password;
        this.avatar = avatar;
        this.status = status;
        this.intro = intro;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;


    @Column(name = "username")
    private String username;

    @Column(name = "name")
    private String name;

    @Column(name = "gender")
    private String gender;

    @JsonIgnoreProperties
    @OneToOne(cascade = CascadeType.ALL, optional = true)
    @JoinColumn(name = "oid")
    private Com com;

    @Column(name = "password")
    private String password;

    @Column(name = "avatar")
    private String avatar = "http://localhost:8080/1.jpg";

    @Column(name="status")
    private String status;

    @Column(name="intro")
    private String intro;

    @JsonIgnoreProperties
    @ManyToMany
    @JoinTable(
            name = "user_roles",
            joinColumns = { @JoinColumn(name = "uid", referencedColumnName = "id") },
            inverseJoinColumns = { @JoinColumn(name = "rid", referencedColumnName = "id") }
    )
    private List<Role> roles;


    @Transient
    private String positions;

    public static User parseMap(Map<String, Object> data) {
        System.out.println(data.get("id"));
        return new User(
            (String) data.get("username"),
            (String) data.get("name"),
            (String) data.get("gender"),
            (String) data.get("password"),
            (String) data.get("avatar"),
            (String) data.get("status"),
            (String) data.get("intro")
        );
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        for(Role role:roles){
            authorities.add(new SimpleGrantedAuthority(role.getRole()));
        }

        return authorities;
    }

    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getIntro() {
        return intro;
    }

    public void setIntro(String intro) {
        this.intro = intro;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }


    public List<Role> getRoles() {
        return roles;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getPositions() {
        return positions;
    }


    public String getRoleString() {
        String str = "[";
        for(Role r:roles){
            str += r.getRole() + ", ";
        }
        if(str.lastIndexOf(",") != -1)
            str = str.substring(0,str.lastIndexOf(","));
        str += "]";
        return str;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }


    public Com getCom() {
        return com;
    }

    public void setCom(Com com) {
        this.com = com;
    }


    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", password='" + password + '\'' +
                ", avatar='" + avatar + '\'' +
                ", status='" + status + '\'' +
                ", intro='" + intro + '\'' +
                ", roles=" + roles +
                '}';
    }
    public String toSimpleString() {
        return "{" +
                "id:" + id +
                ", username:'" + username + '\'' +
                ", name:'" + name + '\'' +
                ", avatar:'" + avatar + '\'' +
                '}';
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
        this.avatar = "http://localhost:8090/img/1.jpg";
    }

    public Map<String,Object> getBasicInfo() {
        Map<String,Object> res = new HashMap<>();
        res.put("id",id);
        res.put("username",username);
        res.put("name",name);
        res.put("avatar",avatar);
        return res;
    }
}
